package com.example.project22_editingmediaplayer.adapter;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.project22_editingmediaplayer.R;
import com.example.project22_editingmediaplayer.adapter.RowSongplayallsong.RowHolder;
import com.example.project22_editingmediaplayer.objects.SongObject;
import com.example.project22_editingmediaplayer.resource.SongResource;

public class RowSongOfRating extends ArrayAdapter<SongObject> {
	public ArrayList<SongObject> listData;

	public final ArrayList<Integer> listselected;
	private final Context context;
	boolean checkAll_flag = false;
	boolean checkItem_flag = false;

	public RowSongOfRating(ArrayList<SongObject> listData, Context mContext) {
		super(mContext, R.layout.row_itemrating, listData);
		this.listData = listData;
		this.context = mContext;
		listselected = new ArrayList<Integer>();
	}

	public void resetData(ArrayList<SongObject> listData) {
		this.listData = listData;
		this.notifyDataSetChanged();
	}

	public void addData(SongObject songObject) {
		listData.add(songObject);
		this.notifyDataSetChanged();
	}

	public ArrayList<SongObject> getAllData() {
		return this.listData;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		try {
			if (!listData.isEmpty())
				return listData.size();
			else
				return 0;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;

	}

	@Override
	public SongObject getItem(int position) {
		// TODO Auto-generated method stub
		return listData.get(position);
	}

	@Override
	public long getItemId(int postition) {
		// TODO Auto-generated method stub
		return postition;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		RowHolder viewHolder = null;

		if (convertView == null) {
			LayoutInflater inflator = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflator.inflate(R.layout.row_itemrating, null);
			viewHolder = new RowHolder();
			viewHolder.imvAvarSong = (ImageView) convertView
					.findViewById(R.id.imvAvarSong);
			viewHolder.txvSong = (TextView) convertView
					.findViewById(R.id.txtNameSong);
			viewHolder.txvArtis = (TextView) convertView
					.findViewById(R.id.txtArtis);
			viewHolder.txtnumber = (TextView) convertView
					.findViewById(R.id.numberofplay);
			viewHolder.star1 = (ImageView) convertView.findViewById(R.id.star1);
			viewHolder.star2 = (ImageView) convertView.findViewById(R.id.star2);
			viewHolder.star3 = (ImageView) convertView.findViewById(R.id.star3);
			viewHolder.star4 = (ImageView) convertView.findViewById(R.id.star4);
			viewHolder.star5 = (ImageView) convertView.findViewById(R.id.star5);

			convertView.setTag(viewHolder);
			// convertView.setTag(R.id.imvAvarSong, viewHolder.imvAvarSong);
			// convertView.setTag(R.id.txtNameSong, viewHolder.txvSong);
			// convertView.setTag(R.id.txtArtis, viewHolder.txvArtis);
			// convertView.setTag(R.id.star1, viewHolder.star1);
			// convertView.setTag(R.id.star2, viewHolder.star2);
			// convertView.setTag(R.id.star3, viewHolder.star3);
			// convertView.setTag(R.id.star4, viewHolder.star4);
			// convertView.setTag(R.id.star5, viewHolder.star5);
			// convertView.setTag(R.id.numberofplay, viewHolder.txtnumber);
		} else {
			viewHolder = (RowHolder) convertView.getTag();
		}
		final SongObject songObject = listData.get(position);
		viewHolder = (RowHolder) convertView.getTag();
		viewHolder.txvSong.setText(songObject.get_name());
		viewHolder.txvArtis.setText(songObject.get_artist());

		Random rand = new Random();
		int currentSongIndex = rand
				.nextInt(SongResource.musicBackGround.length);
		viewHolder.imvAvarSong
				.setBackgroundResource(SongResource.musicBackGround[currentSongIndex]);
		viewHolder.imvAvarSong.setPadding(20, 20, 20, 20);

		viewHolder.txtnumber.setText(String.valueOf(songObject.get_rating()));
		if (songObject.get_rating() < 4) {
			viewHolder.star1.setImageResource(R.drawable.darkstar);
			viewHolder.star5.setImageResource(R.drawable.darkstar);
			viewHolder.star4.setImageResource(R.drawable.darkstar);
			viewHolder.star3.setImageResource(R.drawable.darkstar);
			viewHolder.star2.setImageResource(R.drawable.darkstar);
		}
		if (songObject.get_rating() > 3 && songObject.get_rating() < 5) {
			viewHolder.star1.setImageResource(R.drawable.star);
			viewHolder.star5.setImageResource(R.drawable.darkstar);
			viewHolder.star4.setImageResource(R.drawable.darkstar);
			viewHolder.star3.setImageResource(R.drawable.darkstar);
			viewHolder.star2.setImageResource(R.drawable.darkstar);
		}
		if (songObject.get_rating() > 4 && songObject.get_rating() < 8) {
			viewHolder.star2.setImageResource(R.drawable.star);
			viewHolder.star1.setImageResource(R.drawable.star);
			viewHolder.star5.setImageResource(R.drawable.darkstar);
			viewHolder.star4.setImageResource(R.drawable.darkstar);
			viewHolder.star3.setImageResource(R.drawable.darkstar);
		}
		if (songObject.get_rating() > 7 && songObject.get_rating() < 12) {
			viewHolder.star3.setImageResource(R.drawable.star);
			viewHolder.star2.setImageResource(R.drawable.star);
			viewHolder.star5.setImageResource(R.drawable.darkstar);
			viewHolder.star4.setImageResource(R.drawable.darkstar);
			viewHolder.star1.setImageResource(R.drawable.star);
		}

		if (songObject.get_rating() > 11 && songObject.get_rating() < 20) {
			viewHolder.star4.setImageResource(R.drawable.star);
			viewHolder.star3.setImageResource(R.drawable.star);
			viewHolder.star5.setImageResource(R.drawable.darkstar);
			viewHolder.star2.setImageResource(R.drawable.star);
			viewHolder.star1.setImageResource(R.drawable.star);
		}
		if (songObject.get_rating() > 19) {
			viewHolder.star5.setImageResource(R.drawable.star);
			viewHolder.star4.setImageResource(R.drawable.star);
			viewHolder.star3.setImageResource(R.drawable.star);
			viewHolder.star2.setImageResource(R.drawable.star);
			viewHolder.star1.setImageResource(R.drawable.star);
		} else {
		}
		return convertView;
	}

	public static final class RowHolder {
		public ImageView imvAvarSong;
		public TextView txvSong;
		public TextView txvArtis;
		public ImageView star1;
		public ImageView star2;
		public ImageView star3;
		public ImageView star4;
		public ImageView star5;
		public TextView txtnumber;

	}
}
